package com.glory.good_dark.activity;


import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.good_dark.R;
import com.glory.good_dark.adapter.MergeLotAdapter;
import com.glory.good_dark.application.App;
import com.glory.good_dark.entity.MergeEntity;
import com.glory.good_dark.entity.StrIDIPEntity;
import com.glory.good_dark.entity.response.BaseEntity;
import com.glory.good_dark.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import butterknife.BindView;
import butterknife.OnClick;

public class MergeLotActivity extends BaseProjectActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etLot)
    TextView etLot;
    @BindView(R.id.rvLot)
    RecyclerView rvLot;
    @BindView(R.id.btnAdd)
    Button btnAdd;
    private MergeLotAdapter adapter;

    @Override
    protected void EQPReplaceSuccess(int tag, String content) {
        if (tag == 10010){
            etLot.setText(content);
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_merge_lot;
    }

    @Override
    protected void initView() {
        initRecyclerView(rvLot,this);
        adapter = new MergeLotAdapter(null,getSupportFragmentManager());
        rvLot.setAdapter(adapter);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.btnAdd,R.id.etLot})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.btnAdd:
                adapter.addData(new MergeEntity());
                break;
            case R.id.etLot:
                showSpanDialog(10010,"请输入或扫描批次号");
                break;
        }
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("合批");
        freeUI_titleView.setRightListener("提交", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (etLot.getText().toString().isEmpty()){
                    showToast("请先扫描批次号",false);
                    return;
                }

                if (adapter.getData().size() <= 0){
                    showToast("请先添加子批",false);
                    return;
                }
                FreeUI_EntityCallBack<BaseEntity<StrIDIPEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseEntity<StrIDIPEntity>>
                        (new TypeToken<BaseEntity<StrIDIPEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess
                            (final Response<BaseEntity<StrIDIPEntity>> response) {
                        if (response.body().isSuccess(getApplicationContext(),getSupportFragmentManager())) {
                            showToast("合批成功",true);
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    MergeLotActivity.this.finish();
                                }
                            },2500);
                        }
                    }

                    @Override
                    public void onError
                            (Response<BaseEntity<StrIDIPEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "PDAMergeLots");
                    }
                };
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("MOTHER_LOTID", etLot.getText().toString());
                jsonObject.add("SUB_LOTIDS",new JsonParser().parse(new Gson().toJson(adapter.getData())).getAsJsonArray());
                NetUtils.requestPostNet(App.WEB_URL + "/api/PDA", this, callBack, "PDAMergeLots", jsonObject);

            }
        });
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }
}